<template>
  <a-form-model
    ref="detailForm"
    :model="detailForm"
    :rules="detailRule"
    :label-col="labelCol"
    :wrapper-col="wrapperCol"
  >
    <a-form-model-item label="文件名称" prop="beanName" help="文件名称,前端包名;后端包名文件名">
      <a-input v-model="detailForm.beanName" />
    </a-form-model-item>
    <a-form-model-item label="生成模块" prop="moduleName" help="模块的名称，请选择项目中已存在的模块">
      <a-select option-label-prop="label" v-model="detailForm.moduleName">
        <a-select-option
          v-for="item in moduleList"
          :key="item.moduleId"
          :label="item.path"
          :value="item.moduleName"
        >
          <span style="float: left">{{ item.moduleName }}</span>
          <span style="float: right; color: #8492a6; font-size: 13px">{{ item.path }}</span>
        </a-select-option>
      </a-select>
    </a-form-model-item>
    <a-form-model-item label="后端包名" prop="packageName" help="Controller,Service生成路径,可自己适当调整放包路径">
      <a-input v-model="detailForm.packageName" />
    </a-form-model-item>
    <a-form-model-item label="生成类型" prop="createType">
      <a-select v-model="detailForm.createType">
        <a-select-opt-group v-for="(item,index) in createTypeList" :key="index" :label="item.label">
          <a-select-option
            v-for="option in item.options"
            :key="option.key"
            :value="option.value"
          >{{option.label}}</a-select-option>
        </a-select-opt-group>
      </a-select>
    </a-form-model-item>
    <a-form-model-item label="Vue生成地址" prop="vueCreatePath" help="Vue文件生成地址,页面组件">
      <a-input v-model="detailForm.vueCreatePath" disabled />
    </a-form-model-item>
    <a-form-model-item label="api生成地址" prop="apiCreatePath" help="api生成地址,存放前端请求">
      <a-input v-model="detailForm.apiCreatePath" disabled />
    </a-form-model-item>
    <a-form-model-item label="profile生成地址" prop="vueCreatePath" help="profile配置文件生成地址,页面组件">
      <a-input v-model="detailForm.profileCreatePath" disabled />
    </a-form-model-item>
    <a-form-model-item label="java生成地址" prop="javaCreatePath" help="Controller,Service生成地址">
      <a-input v-model="detailForm.javaCreatePath" disabled />
    </a-form-model-item>
    <a-form-model-item label="sql生成地址" prop="sqlCreatePath" help="sql生成地址">
      <a-input v-model="detailForm.sqlCreatePath" disabled />
    </a-form-model-item>
  </a-form-model>
</template>
<script>
import { underlineToHump, replaceAll } from "@/utils/tool-str";
import { moduleList } from "@/api/org/module";
const createServicePath = "E:/project/future-ant/server";
const createWebPath = "E:/project/future-ant/web";
//生成类型
const createTypeList = [
  {
    label: "全部生成",
    options: [{ label: "ALL", value: "all", key: "ALL" }]
  },
  {
    label: "前/后端生成",
    options: [
      { label: "SERVER", value: "server", key: "SERVER" },
      { label: "WEB", value: "web", key: "WEB" }
    ]
  },
  {
    label: "单个生成",
    options: [
      { label: "JAVA", value: "java", key: "JAVA" },
      { label: "SQL", value: "sql", key: "SQL" },
      { label: "VUE", value: "vue", key: "VUE" },
      { label: "API", value: "api", key: "API" }
    ]
  }
];

const detailRule = {
  beanName: [{ required: true, trigger: "blur", message: "请填写文件名称" }],
  moduleName: [
    { required: true, trigger: "change", message: "请选择生成模块" }
  ],
  packageName: [{ required: true, trigger: "blur", message: "请填写后端包名" }],
  createType: [{ required: true, trigger: "change", message: "请选择生成类型" }]
};
export default {
  props: {
    tableName: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      detailForm: {
        moduleName: null,
        packageName: null
      },
      detailRule,
      moduleList: [],
      createTypeList,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 }
    };
  },
  created() {
    this.detailForm.tableName = this.tableName;
    this.detailForm.beanName = underlineToHump(
      this.tableName.substring(this.tableName.indexOf("_") + 1)
    );
    this.getModuleList();
  },
  methods: {
    getModuleList() {
      moduleList().then(response => {
        if (response.data.code == 60000) {
          this.moduleList = response.data.data;
        }
      });
    }
  },
  watch: {
    "detailForm.moduleName": function(val) {
      let module = this.moduleList.filter(m => m.moduleName == val)[0];
      this.detailForm.packageName = module.package; //得到包名
      //api生成路径
      this.detailForm.apiCreatePath =
        createWebPath + "/src/api/" + module.moduleName;

      //profile文件生成路径
      this.detailForm.profileCreatePath =
        createWebPath + "/src/profile/" + module.moduleName;

      //vue生成路径
      this.detailForm.vueCreatePath =
        createWebPath +
        "/src/views/" +
        module.moduleName +
        "/" +
        this.detailForm.beanName;

      //java文件生成路径
      this.detailForm.javaCreatePath =
        createServicePath +
        module.path +
        "/src/main/java/" +
        replaceAll(this.detailForm.packageName, ".", "/") +
        "/" +
        this.detailForm.beanName;
      //sql文件生成路径
      this.detailForm.sqlCreatePath =
        createServicePath +
        module.path +
        "/src/main/resources" +
        module.path +
        "/sql";
    },
    "detailForm.packageName": function(val) {
      var _self = this;
      var module = this.moduleList.filter(
        m => m.moduleName == _self.detailForm.moduleName
      )[0];
      //java文件生成路径
      this.detailForm.javaCreatePath =
        createServicePath +
        module.path +
        "/src/main/java/" +
        replaceAll(val, ".", "/") +
        "/" +
        this.detailForm.beanName;
    }
  }
};
</script>
